package com.mailsystem.www.dao;

import com.mailsystem.www.model.Users;
import com.mailsystem.www.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class UserDaoImpl extends BaseDaoImpl<Users> implements UserDao{
    Users user = null;
    Connection conn =null;
    PreparedStatement pstm=null;
    ResultSet rs=null;
    @Override
    public Users findUserbyNameandPassword(Users loginUser){
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from users where username = ? and password = ?";
            pstm = conn.prepareStatement(sql);
            pstm.setString(1, loginUser.getUsername());
            pstm.setString(2, loginUser.getPassword());
            rs = pstm.executeQuery();
            if(rs.next()) {
                user = new Users();
                user.setId(rs.getInt(1));
                user.setUsername(rs.getString(2));
                user.setPassword(rs.getString(3));
            }
        }catch (SQLException e) {
            throw new RuntimeException(e);
        }finally{
            JDBCUtils.close(rs,pstm,conn);
            return user;
        }
    }
}
